/*
 * FlightReservation.java
 *
 * Created on __DATE__, __TIME__
 */

package com.my.test.swing;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.ComboBoxModel;
import javax.swing.JOptionPane;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListDataListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.AbstractTableModel;

import com.my.test.swing.util.TableSorter;


/**
 *
 * @author  __USER__
 */
public class FlightReservation extends javax.swing.JFrame 
	implements ActionListener, ListSelectionListener{

	/** Creates new form FlightReservation */
	public FlightReservation() {
		initComponents();
	}

	/** This method is called from within the constructor to
	 * initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is
	 * always regenerated by the Form Editor.
	 */
	//GEN-BEGIN:initComponents
	// <editor-fold defaultstate="collapsed" desc="Generated Code">
	private void initComponents() {

		jLabel2 = new javax.swing.JLabel();
		jLabel1 = new javax.swing.JLabel();
		jComboBox1 = new javax.swing.JComboBox();
		jLabel3 = new javax.swing.JLabel();
		jComboBox2 = new javax.swing.JComboBox();
		jButton1 = new javax.swing.JButton();
		jScrollPane1 = new javax.swing.JScrollPane();
		jTable1 = new javax.swing.JTable();
		jLabel4 = new javax.swing.JLabel();
		jTextField1 = new javax.swing.JTextField();
		jButton2 = new javax.swing.JButton();
		fliTabModel = new FlightTableModel();
		TableSorter tabSorter = new TableSorter(fliTabModel, jTable1.getTableHeader());
		
		this.setLocation(300, 400);

		jComboBox2.setModel(new CityComboModel(DataHandler.getCities()));

		jLabel2.setText("jLabel2");

		setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
		setTitle("Flight");

		jLabel1.setText("\u51fa\u53d1\u57ce\u5e02");

		jComboBox1.setModel(new CityComboModel(DataHandler.getCities()));
		

		jLabel3.setText("\u76ee\u6807\u57ce\u5e02");

		jButton1.setText("\u641c\u7d22");
		jButton1.setActionCommand("search");
		jButton1.addActionListener(this);

		jTable1.setModel(tabSorter);
		jTable1.getSelectionModel().addListSelectionListener(this);
		jTable1.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		jTable1.setDefaultRenderer(Object.class, new FlightTableRenderer());
		
		jScrollPane1.setViewportView(jTable1);

		jLabel4.setText("\u673a\u7968\u6570\u91cf");


		jButton2.setText("\u9884\u5b9a");
		jButton2.setActionCommand("purchase");
		jButton2.addActionListener(this);

		jButton2.setEnabled(false);
		jTextField1.setEnabled(false);
		
		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
				getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(layout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(
						layout.createSequentialGroup()
								.addGap(117, 117, 117)
								.addComponent(jLabel1)
								.addGap(18, 18, 18)
								.addComponent(jComboBox1,
										javax.swing.GroupLayout.PREFERRED_SIZE,
										javax.swing.GroupLayout.DEFAULT_SIZE,
										javax.swing.GroupLayout.PREFERRED_SIZE)
								.addGap(45, 45, 45)
								.addComponent(jLabel3)
								.addGap(18, 18, 18)
								.addComponent(jComboBox2,
										javax.swing.GroupLayout.PREFERRED_SIZE,
										javax.swing.GroupLayout.DEFAULT_SIZE,
										javax.swing.GroupLayout.PREFERRED_SIZE)
								.addPreferredGap(
										javax.swing.LayoutStyle.ComponentPlacement.RELATED,
										42, Short.MAX_VALUE)
								.addComponent(jButton1).addGap(31, 31, 31))
				.addGroup(
						javax.swing.GroupLayout.Alignment.TRAILING,
						layout.createSequentialGroup()
								.addContainerGap(52, Short.MAX_VALUE)
								.addComponent(jScrollPane1,
										javax.swing.GroupLayout.PREFERRED_SIZE,
										452,
										javax.swing.GroupLayout.PREFERRED_SIZE)
								.addContainerGap())
				.addGroup(
						layout.createSequentialGroup()
								.addGap(82, 82, 82)
								.addComponent(jLabel4)
								.addGap(30, 30, 30)
								.addComponent(jTextField1,
										javax.swing.GroupLayout.PREFERRED_SIZE,
										45,
										javax.swing.GroupLayout.PREFERRED_SIZE)
								.addGap(83, 83, 83).addComponent(jButton2)
								.addContainerGap(171, Short.MAX_VALUE)));
		layout.setVerticalGroup(layout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(
						layout.createSequentialGroup()
								.addGap(31, 31, 31)
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.TRAILING)
												.addGroup(
														layout.createParallelGroup(
																javax.swing.GroupLayout.Alignment.BASELINE)
																.addComponent(
																		jComboBox2,
																		javax.swing.GroupLayout.PREFERRED_SIZE,
																		javax.swing.GroupLayout.DEFAULT_SIZE,
																		javax.swing.GroupLayout.PREFERRED_SIZE)
																.addComponent(
																		jButton1))
												.addComponent(jLabel3)
												.addComponent(
														jComboBox1,
														javax.swing.GroupLayout.PREFERRED_SIZE,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														javax.swing.GroupLayout.PREFERRED_SIZE)
												.addComponent(jLabel1))
								.addPreferredGap(
										javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
								.addComponent(jScrollPane1,
										javax.swing.GroupLayout.PREFERRED_SIZE,
										229,
										javax.swing.GroupLayout.PREFERRED_SIZE)
								.addPreferredGap(
										javax.swing.LayoutStyle.ComponentPlacement.RELATED,
										24, Short.MAX_VALUE)
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.BASELINE)
												.addComponent(jButton2)
												.addComponent(jLabel4)
												.addComponent(
														jTextField1,
														javax.swing.GroupLayout.PREFERRED_SIZE,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														javax.swing.GroupLayout.PREFERRED_SIZE))
								.addGap(27, 27, 27)));

		pack();
	}// </editor-fold>
	//GEN-END:initComponents

	private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {
		// TODO add your handling code here:
	}

	/**
	 * @param args the command line arguments
	 */
	public static void main(String args[]) {
		java.awt.EventQueue.invokeLater(new Runnable() {
			public void run() {
				new FlightReservation().setVisible(true);
			}
		});
	}

	//GEN-BEGIN:variables
	// Variables declaration - do not modify
	private javax.swing.JButton jButton1;
	private javax.swing.JButton jButton2;
	private javax.swing.JComboBox jComboBox1;
	private javax.swing.JComboBox jComboBox2;
	private javax.swing.JLabel jLabel1;
	private javax.swing.JLabel jLabel2;
	private javax.swing.JLabel jLabel3;
	private javax.swing.JLabel jLabel4;
	private javax.swing.JScrollPane jScrollPane1;
	private javax.swing.JTable jTable1;
	private javax.swing.JTextField jTextField1;
	private FlightTableModel fliTabModel;

	// End of variables declaration//GEN-END:variables

	private class CityComboModel implements ComboBoxModel {

		private List data = new ArrayList();
		private int selected = 0;

		public CityComboModel(List list) {
			data = list;
		}

		@Override
		public void addListDataListener(ListDataListener arg0) {
			// TODO Auto-generated method stub

		}

		@Override
		public Object getElementAt(int arg0) {
			// TODO Auto-generated method stub
			return data.get(arg0);
		}

		@Override
		public int getSize() {
			// TODO Auto-generated method stub
			return data.size();
		}

		@Override
		public void removeListDataListener(ListDataListener arg0) {
			// TODO Auto-generated method stub

		}

		@Override
		public Object getSelectedItem() {
			// TODO Auto-generated method stub
			return data.get(selected);
		}

		@Override
		public void setSelectedItem(Object anItem) {
			// TODO Auto-generated method stub
			selected = data.indexOf(anItem);
		}

	}

	private class FlightTableModel extends AbstractTableModel {

		private List data = new ArrayList();

		public void upadteData(List list) {
			data = list;
			super.fireTableDataChanged();//通知所有侦听器，表的所有行单元格值可能已更改。
		}

		@Override
		public int getColumnCount() {
			// TODO Auto-generated method stub
			return 4;
		}

		@Override
		public int getRowCount() {
			// TODO Auto-generated method stub
			return data.size();
		}

		@Override
		public Object getValueAt(int arg0, int arg1) {
			// TODO Auto-generated method stub
			Flight o = (Flight) data.get(arg0);
			if (arg1 == 0) {
				return o.getSrc();
			} else if (arg1 == 1) {
				return o.getDesc();
			} else if (arg1 == 2) {
				return o.getFlightNo();
			} else {
				return o.getRemainTic();
			}
		}

		@Override
		public String getColumnName(int column) {
			// TODO Auto-generated method stub
			if (column == 0) {
				return "出发地";
			} else if (column == 1) {
				return "目的地";
			} else if (column == 2) {
				return "航班号";
			} else {
				return "剩余票数";
			}
		}

		public List getData() {
			return data;
		}

		public void setData(List data) {
			this.data = data;
		}

		@Override
		public boolean isCellEditable(int rowIndex, int columnIndex) {
			// TODO Auto-generated method stub
			return columnIndex == 3;
		}

		@Override
		public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
			// TODO Auto-generated method stub
			if(columnIndex == 3){
				Flight fli = (Flight) data.get(rowIndex);
				fli.setRemainTic(Integer.valueOf((String)aValue));
			}
		}

		
		

	}

	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		String command = e.getActionCommand();
		if("search".equals(command)){
			String src = (String) jComboBox1.getSelectedItem();
			String desc = (String) jComboBox2.getSelectedItem();
			List list = DataHandler.searchRecord(src, desc);
			System.out.println(list.size());
			fliTabModel.upadteData(list);
		}else if("purchase".equals(command)){
			Flight f = (Flight) fliTabModel.getData().get(jTable1.getSelectedRow());
			String ticNum = jTextField1.getText();
			try {
				DataHandler.updateRecord(f, Integer.valueOf(ticNum));
			}  catch (Exception e1) {
				// TODO Auto-generated catch block
				JOptionPane.showMessageDialog(this, e1.getMessage(), "错误", 
						JOptionPane.ERROR_MESSAGE);
			}
		}
	}

	@Override
	public void valueChanged(ListSelectionEvent e) {
		// TODO Auto-generated method stub
		boolean selected = jTable1.getSelectedRow() > -1;
		jButton2.setEnabled(selected);
		jTextField1.setEnabled(selected);
	}

}